import request from '@/utils/request';
import { download, toFormData, checkDownloadRes } from '@/utils/common';

/**
 * 分页查询染色领料单
 */
export async function pageMesDyeingMaterialRequisitions(params) {
  const res = await request.get('/mes/mesDyeingMaterialRequisition/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部染色领料单
 */
export async function listMesDyeingMaterialRequisitions(params) {
  const res = await request.get('/mes/mesDyeingMaterialRequisition/list', { params });
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询染色领料单
 */
export async function getMesDyeingMaterialRequisition(id) {
  const res = await request.get('/mes/mesDyeingMaterialRequisition/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加染色领料单
 */
export async function addMesDyeingMaterialRequisition(data) {
  const res = await request.post('/mes/mesDyeingMaterialRequisition', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改染色领料单
 */
export async function updateMesDyeingMaterialRequisition(data) {
  const res = await request.put('/mes/mesDyeingMaterialRequisition', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除染色领料单
 */
export async function removeMesDyeingMaterialRequisition(id) {
  const res = await request.delete('/mes/mesDyeingMaterialRequisition/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除染色领料单
 */
export async function removeMesDyeingMaterialRequisitions(ids) {
  const res = await request.delete('/mes/mesDyeingMaterialRequisition/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出染色领料单
 */
export async function exportMesDyeingMaterialRequisitions(params) {
  const res = await request({
    url: '/mes/mesDyeingMaterialRequisition/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `mesDyeingMaterialRequisition_${Date.now()}.xlsx`);
}
